package com.weimengchao.example;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.weimengchao.common.algorithm.SnowflakeManager;
import com.weimengchao.common.constant.LogicDelete;
import com.weimengchao.core.http.PageResult;
import com.weimengchao.example.dao.OrderInfoDao;
import com.weimengchao.example.entity.OrderInfo;
import com.weimengchao.example.mapper.OrderInfoMapper;
import com.weimengchao.mybatisplus.mapping.PageMapping;
import org.junit.jupiter.api.Test;

import javax.annotation.Resource;
import java.util.List;

public class Demo extends MybatisPlusExampleApplicationTests {

    @Resource
    OrderInfoDao orderInfoDao;
    @Resource
    OrderInfoMapper orderInfoMapper;

    @Test
    public void save() throws Exception {
        SnowflakeManager snowflakeManager = new SnowflakeManager(0, 0);
        OrderInfo orderInfo = new OrderInfo();
        orderInfo.setOrderId(snowflakeManager.nextValue());
        orderInfoDao.save(orderInfo);
    }

    @Test
    public void page() {
        Page page = new Page<>(1, 20);
        QueryWrapper<OrderInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(OrderInfo::getLogicDelete, LogicDelete.NOT_DELETE.getState());
        IPage<OrderInfo> iPage = orderInfoDao.page(page, queryWrapper);
        PageResult<OrderInfo> orderInfoPageResult = PageMapping.INSTANCE.iPage2PageResult(iPage);
        System.out.println(orderInfoPageResult);
    }

    @Test
    public void select() {
        QueryWrapper<OrderInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(OrderInfo::getOrderId, 1548436388574134279L);
        List<OrderInfo> orderInfos = orderInfoMapper.selectList(queryWrapper);
        System.out.println(orderInfos);
    }

}